/*************************************************************************
 *  Copyright © 2023-2030 FXB CO.,LTD. All rights reserved.
 *------------------------------------------------------------------------
 *  公司：DefaultCompany
 *  项目：CustomBuildProcess
 *  文件：BuildLogUpgrade.cs
 *  作者：Administrator
 *  日期：2025/6/19 20:24:28
 *  功能：Nothing
*************************************************************************/

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

namespace CustomBuildProcess.Editor
{
    /// <summary>
    /// 旧版升级
    /// </summary>
    public class BuildLogUpgrade
    {
        /// <summary>
        /// 升级，把日志全部重新往里写一遍
        /// </summary>
        [MenuItem("Edit/Build Log/Upgrade")]
        public static async Task UpgradeAsync()
        {
            string htmlLogTemplatePath = PathManager.BuildLogHtmlTemplatePath;
            string LogItemTemplatePath = PathManager.LogItemHtmlTemplatePath;

            string htmlLogPath = PathManager.BuildLogHtmlPath;

            try
            {
                string htmlContent = LogInfoHtmlOutput.BuildHtmlHeader(htmlLogTemplatePath);

                //获取到所有的日志信息，循环写入
                BuildConfigItem logConfigItem = Resources.Load<BuildConfigItem>(PathManager.BuildConfigItemPath);
                BuildInfoConfig configAsset = AssetDatabase.LoadAssetAtPath<BuildInfoConfig>(PathManager.BuildConfigPath);

                string newLogBlock = "";

                for (int i = configAsset.buildInfos.Count - 1; i >= 0; i--)
                {
                    BuildInfo info = configAsset.buildInfos[i];
                    newLogBlock += "\n" + LogInfoHtmlOutput.BuildLogHtml(LogItemTemplatePath, info, logConfigItem);
                }

                string contentTag = $"<div class=\"content\">";

                // 查找该标签在 HTML 中的位置
                int contentStartIndex = htmlContent.IndexOf(contentTag, StringComparison.Ordinal);

                if (contentStartIndex >= 0)
                {
                    int contentEndIndex = contentStartIndex + contentTag.Length;

                    htmlContent = htmlContent.Insert(contentEndIndex, "\n" + newLogBlock);
                }
                else
                {
                    int bodyStartIndex = htmlContent.IndexOf("</body>", StringComparison.Ordinal);
                    if (bodyStartIndex >= 0)
                    {
                        htmlContent = htmlContent.Insert(bodyStartIndex, "\n" + newLogBlock);
                    }
                }

                htmlContent = LogInfoHtmlOutput.RemoveEmptyLines(htmlContent);

                // 写入文件
                using (StreamWriter writer = new StreamWriter(htmlLogPath, false, Encoding.UTF8))
                {
                    await writer.WriteAsync(htmlContent);
                }

                Debug.Log($"Build log upgrade successful");

                AssetDatabase.Refresh();
            }
            catch (Exception e)
            {
                Debug.LogError($"Build log upgrade failed {e.Message}");
            }
        }
    }
}